﻿Imports System.Threading
Imports WindowsApplication1.GoogleAnalyticsMailHandler

Public Delegate Sub OnEmailsRecived(ByVal sender As Object, ByVal emails As List(Of Email))
Public Delegate Sub OnErrorOccured(ByVal sender As Object, ByVal errorItem As Exception)

Public Class EmailListener

    Private Const CONNECTING As Integer = 0
    Private Const CONNECTED As Integer = 1
    Private Const DISCONNECTING As Integer = 2
    Private Const DISCONNECTED As Integer = 3

    Private thread As Thread
    Private m_status As Integer
    Private m_lastEmailCount As Integer


    Public Property Host() As String
        Get
            Return m_Host
        End Get
        Protected Set(ByVal value As String)
            m_Host = value
        End Set
    End Property
    Private m_Host As String
    Public Property Port() As Integer
        Get
            Return m_Port
        End Get
        Protected Set(ByVal value As Integer)
            m_Port = value
        End Set
    End Property
    Private m_Port As Integer
    Public Property Email() As String
        Get
            Return m_Email
        End Get
        Protected Set(ByVal value As String)
            m_Email = value
        End Set
    End Property
    Private m_Email As String
    Public Property Password() As String
        Get
            Return m_Password
        End Get
        Protected Set(ByVal value As String)
            m_Password = value
        End Set
    End Property
    Private m_Password As String
    Public Property IsSecure() As Boolean
        Get
            Return m_IsSecure
        End Get
        Protected Set(ByVal value As Boolean)
            m_IsSecure = value
        End Set
    End Property
    Private m_IsSecure As Boolean
    Public Property WaitTime() As Integer
        Get
            Return m_waitTime
        End Get
        Protected Set(ByVal value As Integer)
            m_waitTime = value
        End Set
    End Property
    Private m_waitTime As Integer

    Public Event EmailsRecived As OnEmailsRecived
    Public Event ErrorOccured As OnErrorOccured

    Public Sub New(ByVal hostParam As String, ByVal portParam As Integer, _
                   ByVal emailParam As String, ByVal passwordParam As String, _
                   ByVal secure As Boolean, ByVal waitTimeParam As Integer)
        Host = hostParam
        Port = portParam
        Email = emailParam
        Password = passwordParam
        IsSecure = secure
        WaitTime = waitTimeParam

        m_status = DISCONNECTED
    End Sub

    Public Sub StopListening()
        If m_status = CONNECTED Or m_status = CONNECTING Then
            m_status = DISCONNECTING
            While m_status = DISCONNECTING
                thread.CurrentThread.Sleep(10)
            End While
        End If
    End Sub

    Public Sub StartListening()
        StopListening()
        thread = New Thread(AddressOf ThreadStarterForListener)
        thread.Start()
    End Sub
    Private Sub ThreadStarterForListener()
        Try
            m_status = CONNECTING
            Using client As Pop3Client = New Pop3Client(Host, Port, Email, Password, IsSecure)
                client.Connect()
                m_status = CONNECTED
                m_lastEmailCount = client.GetEmailCount()
            End Using

            While m_status = CONNECTED
                Using client As Pop3Client = New Pop3Client(Host, Port, Email, Password, IsSecure)
                    client.Connect()
                    LookForNewEmails(client)
                End Using

                Dim totalTime As Integer = TimeSpan.FromSeconds(m_waitTime).Ticks / 10000
                Dim sleptTime As Integer = 0
                Dim intermittentSleepIncrement As Integer = 10

                While m_status = CONNECTED And sleptTime < totalTime
                    thread.Sleep(intermittentSleepIncrement)

                    sleptTime += intermittentSleepIncrement
                End While

            End While
            m_status = DISCONNECTED
        Catch ex As Exception
            m_status = DISCONNECTED
            RaiseEvent ErrorOccured(Me, ex)
        End Try

    End Sub

    Private Sub LookForNewEmails(ByVal client As Pop3Client)
        Dim emailCount = client.GetEmailCount()
        If emailCount > m_lastEmailCount Then
            Dim newEmails = client.FetchEmailList(m_lastEmailCount + 1, emailCount - m_lastEmailCount)
            For Each em In newEmails
                em.MsgParts = client.FetchMessageParts(em.EmailId)
            Next
            RaiseEvent EmailsRecived(Me, newEmails)
        End If

        m_lastEmailCount = emailCount
    End Sub

End Class
